
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2016原作者。
 *
 *根据Apache许可证2.0版（“许可证”）许可；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，否则软件
 *根据许可证分发是在“按原样”的基础上分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证
 *许可证下的限制。
 */

package org.springframework.context.support;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * Convenient application context with built-in XML support.
 * This is a flexible alternative to {@link ClassPathXmlApplicationContext}
 * and {@link FileSystemXmlApplicationContext}, to be configured via setters,
 * with an eventual {@link #refresh()} call activating the context.
 *
 * <p>In case of multiple configuration files, bean definitions in later files
 * will override those defined in earlier files. This can be leveraged to
 * intentionally override certain bean definitions via an extra configuration
 * file appended to the list.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 3.0
 * @see #load
 * @see XmlBeanDefinitionReader
 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext
 */
/**
 *具有内置XML支持的方便的应用程序上下文。
 *这是｛@link ClassPathXmlApplicationContext｝的灵活替代方案
 *和{@link-FileSystemXmlApplicationContext}，
 *通过最终的{@link#refresh（）}调用激活上下文。
 *
 *<p>在多个配置文件的情况下，稍后文件中的bean定义
 *将覆盖早期文件中定义的那些。这可以用来
 *通过额外的配置有意覆盖某些bean定义
 *附加到列表中的文件。
 *
 *@作者Juergen Hoeller
 *@作者Chris Beams
 *@自3.0起
 *@见#load
 *@参见XmlBeanDefinitionReader
 *@参见org.springframework.context.annotation.AnnotationConfigApplicationContext
 */
public class GenericXmlApplicationContext extends GenericApplicationContext {

	private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);


	/**
	 * Create a new GenericXmlApplicationContext that needs to be
	 * {@link #load loaded} and then manually {@link #refresh refreshed}.
	 */
	/**
	 *创建一个新的GenericXmlApplicationContext，该上下文需要
	 *｛@link#load loaded｝，然后手动｛@link#refresh refreshed｝。
	 */
	public GenericXmlApplicationContext() {
	}

	/**
	 * Create a new GenericXmlApplicationContext, loading bean definitions
	 * from the given resources and automatically refreshing the context.
	 * @param resources the resources to load from
	 */
	/**
	 *创建一个新的GenericXmlApplicationContext，加载bean定义
	 *并自动刷新上下文。
	 *@param resources要从中加载的资源
	 */
	public GenericXmlApplicationContext(Resource... resources) {
		load(resources);
		refresh();
	}

	/**
	 * Create a new GenericXmlApplicationContext, loading bean definitions
	 * from the given resource locations and automatically refreshing the context.
	 * @param resourceLocations the resources to load from
	 */
	/**
	 *创建一个新的GenericXmlApplicationContext，加载bean定义
	 *并自动刷新上下文。
	 *@param resource定位要加载的资源
	 */
	public GenericXmlApplicationContext(String... resourceLocations) {
		load(resourceLocations);
		refresh();
	}

	/**
	 * Create a new GenericXmlApplicationContext, loading bean definitions
	 * from the given resource locations and automatically refreshing the context.
	 * @param relativeClass class whose package will be used as a prefix when
	 * loading each specified resource name
	 * @param resourceNames relatively-qualified names of resources to load
	 */
	/**
	 *创建一个新的GenericXmlApplicationContext，加载bean定义
	 *并自动刷新上下文。
	 *@param relativeClass类，当
	 *加载每个指定的资源名称
	 *@param resourceNames要加载的资源的相对限定名称
	 */
	public GenericXmlApplicationContext(Class<?> relativeClass, String... resourceNames) {
		load(relativeClass, resourceNames);
		refresh();
	}


	/**
	 * Exposes the underlying {@link XmlBeanDefinitionReader} for additional
	 * configuration facilities and {@code loadBeanDefinition} variations.
	 */
	/**
	 *公开基础｛@link XmlBeanDefinitionReader｝以获取其他
	 *配置设施和{@code-loadBeanDefinition}变体。
	 */
	public final XmlBeanDefinitionReader getReader() {
		return this.reader;
	}

	/**
	 * Set whether to use XML validation. Default is {@code true}.
	 */
	/**
	 *设置是否使用XML验证。默认值为｛@code true｝。
	 */
	public void setValidating(boolean validating) {
		this.reader.setValidating(validating);
	}

	/**
	 * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}.
	 * Should be called before any call to {@code #load}.
	 */
	/**
	 *将给定的环境委托给基础｛@link XmlBeanDefinitionReader｝。
	 *应在调用｛@code#load｝之前调用。
	 */
	@Override
	public void setEnvironment(ConfigurableEnvironment environment) {
		super.setEnvironment(environment);
		this.reader.setEnvironment(getEnvironment());
	}


	//---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// Convenient methods for loading XML bean definition files
	// 加载XMLbean定义文件的方便方法
	//---------------------------------------------------------------------
	// ---------------------------------------------------------------------

	/**
	 * Load bean definitions from the given XML resources.
	 * @param resources one or more resources to load from
	 */
	/**
	 *从给定的XML资源中加载bean定义。
	 *@param resources一个或多个要加载的资源
	 */
	public void load(Resource... resources) {
		this.reader.loadBeanDefinitions(resources);
	}

	/**
	 * Load bean definitions from the given XML resources.
	 * @param resourceLocations one or more resource locations to load from
	 */
	/**
	 *从给定的XML资源中加载bean定义。
	 *@param resourceLocations一个或多个要加载的资源位置
	 */
	public void load(String... resourceLocations) {
		this.reader.loadBeanDefinitions(resourceLocations);
	}

	/**
	 * Load bean definitions from the given XML resources.
	 * @param relativeClass class whose package will be used as a prefix when
	 * loading each specified resource name
	 * @param resourceNames relatively-qualified names of resources to load
	 */
	/**
	 *从给定的XML资源中加载bean定义。
	 *@param relativeClass类，当
	 *加载每个指定的资源名称
	 *@param resourceNames要加载的资源的相对限定名称
	 */
	public void load(Class<?> relativeClass, String... resourceNames) {
		Resource[] resources = new Resource[resourceNames.length];
		for (int i = 0; i < resourceNames.length; i++) {
			resources[i] = new ClassPathResource(resourceNames[i], relativeClass);
		}
		this.load(resources);
	}

}
